package com.sws.hm.crontab;

import com.sws.hm.dao.EmailDao;
import com.sws.hm.dao.portDao;
import com.sws.hm.dto.port;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;


@Component
public class crontabEmail {

    @Autowired
    portDao socketdao;

    @Autowired
    EmailDao emaildao;

    @Scheduled(cron="${email.schedule}")
    public  void SendEamil() throws IOException, ParseException, EmailException {

        List<port> list=socketdao.findbyStatus();
        List<String>  emailList =emaildao.findEmail();

        int num=list.size();
        if (num>0) {
            HtmlEmail email=new HtmlEmail();//创建一个HtmlEmail实例对象
            email.setHostName("smtp.163.com");//邮箱的SMTP服务器，一般123邮箱的是smtp.123.com,qq邮箱为smtp.qq.com
            email.setCharset("utf-8");//设置发送的字符类型

            for (int i=0;i<emailList.size();i++){
                email.addTo(emailList.get(i));//设置收件人
            }

            email.setFrom("qushencom@163.com","服务端口监测系统");//发送人的邮箱为自己的，用户名可以随便填
            email.setAuthentication("qushencom@163.com","OQSKSEISPZVTHEMK");//设置发送人到的邮箱和用户名和授权码(授权码是自己设置的)
            email.setSubject("服务端口监测系统-离线通知");//设置发送主题email.setMsg("1234");//设置发送内容email.send();//进行发送
            StringBuffer bodyBf = new StringBuffer();
            bodyBf.append("<!DOCTYPE html>");
            bodyBf.append("<HTML>");
            bodyBf.append("<HEAD>");
            bodyBf.append("<TITLE> 管理员你好</TITLE>");
            bodyBf.append("<meta http-equiv=Content-Type content=text/html; charset=utf-8>");
            bodyBf.append("\t\t<style type=\"text/css\">\n" +
                    "\t\t\t\n" +
                    "\t\t\t\n" +
                    "\t\t\ttable.gridtable{\n" +
                    "\t\t\t\tmargin-bottom: 30px;\n" +
                    "\t\t\t\t}\n" +
                    "\t\t\ttable.gridtable {\n" +
                    "\t\t\t\tfont-family: verdana, arial, sans-serif;\n" +
                    "\t\t\t\tfont-size: 11px;\n" +
                    "\t\t\t\tcolor: #333333;\n" +
                    "\t\t\t\tborder-width: 1px;\n" +
                    "\t\t\t\tborder-color: #666666;\n" +
                    "\t\t\t\tborder-collapse: collapse;\n" +
                    "\t\t\t}\n" +
                    "\n" +
                    "\t\t\ttable.gridtable th {\n" +
                    "\t\t\t\tborder-width: 1px;\n" +
                    "\t\t\t\tpadding: 8px;\n" +
                    "\t\t\t\tborder-style: solid;\n" +
                    "\t\t\t\tborder-color: #666666;\n" +
                    "\t\t\t\tbackground-color: #dedede;\n" +
                    "\t\t\t}\n" +
                    "\n" +
                    "\t\t\ttable.gridtable td {\n" +
                    "\t\t\t\tborder-width: 1px;\n" +
                    "\t\t\t\tpadding: 8px;\n" +
                    "\t\t\t\tborder-style: solid;\n" +
                    "\t\t\t\tborder-color: #666666;\n" +
                    "\t\t\t\tbackground-color: #ffffff;\n" +
                    "\t\t\t}\n" +
                    "\t\t</style>");
            bodyBf.append("</HEAD>");
            bodyBf.append("<BODY>");
            bodyBf.append("<h1 align=center style='color:rgb(5,112,255);margin-bottom: 20px'>服务端口监测系统 </h1>");
            bodyBf.append("<table align=center class=\"gridtable\">");
            bodyBf.append("<tr>\n" +
                    "\t\t\t\t<th>名称</th>\n" +
                    "\t\t\t\t<th>地址</th>\n" +
                    "\t\t\t\t<th>端口</th>\n" +
                    "\t\t\t\t<th>离线时间</th>\n" +
                    "\t\t\t</tr>");
            for (int j=0;j<list.size();j++){
                String ip=list.get(j).getIp();
                String mc=list.get(j).getMc();
                String prot=list.get(j).getProt();
                Date time=list.get(j).getTime();
                DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
                Date date = df.parse(time.toString());
                df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String str = df.format(date);
                bodyBf.append("<tr>\n" +
                        "<td>"+mc+"</td>\n" +
                        "<td>"+ip+"</td>\n" +
                        "<td>"+prot+"</td>\n" +
                        "<td>"+str+"</td>\n" +
                        "</tr>");
            }
            bodyBf.append("</table>");
            bodyBf.append("<HR style=\"FILTER: progid:DXImageTransform.Microsoft.Glow(color=#987cb9,strength=10)\" width=\"80%\" SIZE=1>");
            bodyBf.append("<div class=\"main\" align=\"center\">\n" +
                    "\t\t\t        <p>sws©qushencn@qq.com</p>\n" +
                    "\t\t\t    </div>");
            bodyBf.append("</BODY>");
            bodyBf.append("</HTML>");
            //email.setMsg("您的验证码是:123456");
            email.setHtmlMsg(bodyBf.toString());
            email.send();
            System.out.println("发送成功");
        }else{

        }

    }
}
